#!/bin/bash
# Bootstraps supervisor config

# Include defaults
. /usr/local/etc/valheim/defaults
. /usr/local/etc/valheim/common


main() {
    apply_permissions
    configure_timezone
    setup_syslog
    setup_supervisor_http_server
    setup_status_http_server
    pre_supervisor_hook
    exec /usr/bin/tini -- /usr/local/bin/supervisord -c /usr/local/etc/supervisord.conf
}


# Apply user id and group id
apply_permissions() {
    info "Setting uid:gid of valheim to $PUID:$PGID"
    groupmod -g "${PGID}" -o valheim
    #usermod -u "${PUID}" -o -g valheim valheim
    sed -i -E "s/^(valheim:x):[0-9]+:[0-9]+:(.*)/\\1:$PUID:$PGID:\\2/" /etc/passwd

    touch "$SERVER_STATUS_FILE"

    chown -R valheim:valheim \
        /config \
        /opt/valheim \
        /opt/steamcmd \
        /home/valheim \
        /var/run/valheim \
        "$SERVER_STATUS_FILE"
    chgrp valheim /usr/local/etc/supervisord.conf
}


# Configure timezone
configure_timezone() {
    export TZ
    if [ ! -f "/usr/share/zoneinfo/$TZ" ]; then
        warn "Unknown timezone $TZ - defaulting to Etc/UTC"
        TZ="Etc/UTC"
    fi
    ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime
    echo "$TZ" > /etc/timezone
    info "Setting timezone $TZ"
}


# Configure syslog
setup_syslog() {
    local SYSLOG_ARGS
    local log_msg
    SYSLOG_ARGS="-S -t -O -"

    log_target_msg="stdout"
    if [ -n "$SYSLOG_REMOTE_HOST" ]; then
        SYSLOG_ARGS="$SYSLOG_ARGS -R $SYSLOG_REMOTE_HOST:$SYSLOG_REMOTE_PORT"
        log_target_msg="$SYSLOG_REMOTE_HOST:$SYSLOG_REMOTE_PORT"
        if [ "$SYSLOG_REMOTE_AND_LOCAL" = true ]; then
            SYSLOG_ARGS="$SYSLOG_ARGS -L"
            log_target_msg="$log_target_msg and stdout"
        fi
    fi
    info "Setting up syslogd - logging to $log_target_msg"
    cat > "$supervisor_syslog_conf" <<EOF
[program:syslogd]
user=root
environment=HOME="/root",USER="root",LANG="en_US.UTF-8",PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
command=/usr/local/sbin/syslogd -n $SYSLOG_ARGS
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autostart=true
autorestart=true
priority=10
EOF
}


# Enable/disable supervisor http server
setup_supervisor_http_server() {
    rm -f "$supervisor_http_server_conf"
    if [ "$SUPERVISOR_HTTP" = true ]; then
        info "Supervisor http server activated"
        cat > "$supervisor_http_server_conf" <<EOF
[inet_http_server]
port = :$SUPERVISOR_HTTP_PORT
EOF
        chmod 600 "$supervisor_http_server_conf"
        if [ -n "$SUPERVISOR_HTTP_USER" ] && [ -n "$SUPERVISOR_HTTP_PASS" ]; then
            cat >> "$supervisor_http_server_conf" <<EOF
username = $SUPERVISOR_HTTP_USER
password = $SUPERVISOR_HTTP_PASS
EOF
        fi
    fi
}


# Enable/disable status http server
setup_status_http_server() {
    rm -f "$status_http_server_conf"
    rm -f "$status_http_server_updater_conf"
    if [ "$STATUS_HTTP" = true ]; then
        info "Status http server activated"
        touch "$STATUS_HTTP_CONF"
        mkdir -p "$STATUS_HTTP_HTDOCS"
        if [ ! -d "$STATUS_HTTP_HTDOCS" ]; then
            error "Could not create directory $STATUS_HTTP_HTDOCS"
            return
        fi
        cat > "$status_http_server_conf" <<EOF
[program:valheim-status-httpd]
user=root
environment=HOME="/root",USER="root",LANG="en_US.UTF-8",PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
command=/usr/local/bin/httpd -f -p "$STATUS_HTTP_PORT" -h "$STATUS_HTTP_HTDOCS" -c "$STATUS_HTTP_CONF"
stdout_syslog=true
stderr_syslog=true
stdout_logfile_maxbytes=1MB
stderr_logfile_maxbytes=1MB
autostart=true
autorestart=true
startsecs=10
startretries=0
priority=80
EOF
        chmod 600 "$status_http_server_conf"
        cat > "$status_http_server_updater_conf" <<EOF
[program:valheim-status-updater]
user=root
environment=HOME="/root",USER="root",LANG="en_US.UTF-8",PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
command=/usr/local/bin/valheim-status --update
stdout_syslog=true
stderr_syslog=true
stdout_logfile_maxbytes=1MB
stderr_logfile_maxbytes=1MB
autostart=true
autorestart=true
startsecs=10
startretries=0
priority=70
EOF
        chmod 600 "$status_http_server_updater_conf"
    fi
}


pre_supervisor_hook() {
    if [ -n "$PRE_SUPERVISOR_HOOK" ]; then
        info "Running pre supervisor hook: $PRE_SUPERVISOR_HOOK"
        eval "$PRE_SUPERVISOR_HOOK"
    fi
}


main
